﻿/*
 *  This file is part of Worker Time.
 *
 *  Worker Time is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation, either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  Worker Time is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with Worker Time.  If not, see <http://www.gnu.org/licenses/>.
 */
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Threading;
using WorkerTime.Utils;

namespace WorkerTime.Database
{
    public partial class TrackManagerEdit : Form
    {
        private Manager database;
        public Track track;
        private ComponentResourceManager localization;

        public TrackManagerEdit(Manager database, Track track)
        {
            localization = new System.ComponentModel.ComponentResourceManager(typeof(WorkerTimeStrings));
            this.database = database;
            this.track = track;
            this.Text = localization.GetString("String18") + " " + localization.GetString("String20");
            InitializeComponent();
        }

        private void TrackManagerEdit_Load(object sender, EventArgs e)
        {

            if (track == null)
                track = new Track();

            TrackUtils.PopulateComboBoxProjects(database, comboBoxProject);
            TrackUtils.SelectComboBoxProjectItem(database, comboBoxProject, track.projectId.ToString());
            TrackUtils.PopulateComboBoxActivities(database, comboBoxProject, comboBoxActivity);
            TrackUtils.SelectComboBoxActivityItem(database, comboBoxActivity, track.activityId.ToString());

            dateTimeStart.CustomFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + Thread.CurrentThread.CurrentUICulture.DateTimeFormat.LongTimePattern;
            dateTimeEnd.CustomFormat = Thread.CurrentThread.CurrentCulture.DateTimeFormat.ShortDatePattern + " " + Thread.CurrentThread.CurrentUICulture.DateTimeFormat.LongTimePattern;


            // Verify if track.[startTime|endTime] is a valid DateTime
            if (track.startTime > DateTime.MinValue)
                dateTimeStart.Value = track.startTime;
            else
                track.startTime = DateTime.Now;

            if (track.endTime > DateTime.MinValue)
                dateTimeEnd.Value = track.endTime;
            else
                track.endTime = DateTime.Now;
  
            UpdateDuration(track.ElapsedTime());
        }

        private void UpdateDuration(TimeSpan timeSpan)
        {
            // If it a TimeSpan with some duration
            if (timeSpan.Ticks >= 0)
            {
                DateTime formattedTotal;
                formattedTotal = DateTime.MinValue.Add(timeSpan);
                textBoxDuration.Text = timeSpan.ToString();
                    // String.Format(formattedTotal.ToString("dd HH:mm:ss"));
            }
        }

        private void buttonSave_Click(object sender, EventArgs e)
        {
            if (ValidateStart() && ValidateEnd())
            {
                track.projectId = Int32.Parse(((ComboListItem)comboBoxProject.Items[comboBoxProject.SelectedIndex]).ShortName);
                track.activityId = Int32.Parse(((ComboListItem)comboBoxActivity.Items[comboBoxActivity.SelectedIndex]).ShortName);
                track.startTime = dateTimeStart.Value;
                track.endTime = dateTimeEnd.Value;
                DialogResult = DialogResult.OK;
            }
        }

        private void comboBoxProject_SelectedIndexChanged(object sender, EventArgs e)
        {
            TrackUtils.PopulateComboBoxActivities(database, comboBoxProject, comboBoxActivity);
        }

        private void dateTimeStart_ValueChanged(object sender, EventArgs e)
        {
            UpdateDuration(dateTimeEnd.Value - dateTimeStart.Value);
        }

        private void dateTimeEnd_ValueChanged(object sender, EventArgs e)
        {
            UpdateDuration(dateTimeEnd.Value - dateTimeStart.Value);
        }

        #region Validações

        private void dateTimeStart_Validating(object sender, CancelEventArgs e)
        {
            ValidateStart();            
        }

        private bool ValidateStart()
        {
            if (dateTimeStart.Value >= dateTimeEnd.Value)
            {
                errorProvider.SetError(dateTimeStart, localization.GetString("String21") );
                return false;
            }
            else
            {
                errorProvider.SetError(dateTimeStart, "");
                return true;
            }
        }

        private void dateTimeEnd_Validating(object sender, CancelEventArgs e)
        {
            ValidateEnd();
        }

        private bool ValidateEnd()
        {
            if (dateTimeStart.Value >= dateTimeEnd.Value)
            {
                errorProvider.SetError(dateTimeEnd, localization.GetString("String22"));
                return false;
            }
            else
            {
                errorProvider.SetError(dateTimeEnd, "");
                return true;
            }
        }

        #endregion


    }
}
